package com.zhu.service;

import com.zhu.mapper.UserMapper;
import com.zhu.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public List<User> userList() {
        return userMapper.queryUserList();
    }


    public boolean isExist(String username) {
        User user = userMapper.getUserByName(username);
        return null != user;
    }

    public int getIdByName(String username) {
        return userMapper.getUserByName(username).getId();
    }


    public User getByName(String username) {
        return userMapper.getUserByName(username);
    }

    public User getByUsernameAndPassword(String username, String password) {
        return userMapper.getUserByNameAndPassWord(username, password);
    }

    public boolean add(User user) {
        User userByName = userMapper.getUserByName(user.getUsername());
        if (userByName != null) {
            return false;
        } else {
            int i = userMapper.addUser(user);
            if (i > 0) {
                return true;
            } else {
                return false;
            }
        }

    }

    public boolean isModify(User user) {
        int row = userMapper.updateUser(user);
        if(row > 0) {
            return true;
        }else {
            return false;
        }
    }

    public boolean del(int  id) {
        int row = userMapper.deleteUser(id);
        if(row > 0) {
            return true;
        }else {
            return false;
        }
    }

    public String role(String username) {
        return userMapper.getRoleByName(username);
    }
}
